﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using HotelATM_Guru;

namespace HotelATM_Guru
{
    public class XML
    {
        public static int WriteATMToXML(List<CPosATM> lstDongA, List<CPosATM> lstACB, List<CPosATM> lstVCB)
        {
            try
            {
                XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-16", "true"),
                    new XElement("Banks"));
                XElement root = xdoc.Root;
                // Đông Á
                XElement bank = new XElement("Bank");
                bank.SetAttributeValue("name", "Đông Á Bank");
                root.Add(bank);
                foreach (CPosATM atm in lstDongA)
                    bank.Add(new XElement("ATM", new XAttribute("address", atm.address), new XAttribute("place", atm.place), new XAttribute("image", "ATM.jpg"),
                        new XAttribute("workinghour", atm.workinghour), new XAttribute("lat", atm.lat), new XAttribute("lng", atm.lng)));

                // ACB
                bank = new XElement("Bank");
                bank.SetAttributeValue("name", "ACB Bank");
                root.Add(bank);
                foreach (CPosATM atm in lstACB)
                    bank.Add(new XElement("ATM", new XAttribute("address", atm.address), new XAttribute("place", atm.place), new XAttribute("image", "ATM.jpg"),
                        new XAttribute("workinghour", atm.workinghour), new XAttribute("lat", atm.lat), new XAttribute("lng", atm.lng)));

                // VietComBank
                bank = new XElement("Bank");
                bank.SetAttributeValue("name", "VietComBank");
                root.Add(bank);
                foreach (CPosATM atm in lstVCB)
                    bank.Add(new XElement("ATM", new XAttribute("address", atm.address), new XAttribute("place", atm.place), new XAttribute("image", "ATM.jpg"),
                            new XAttribute("workinghour", atm.workinghour), new XAttribute("lat", atm.lat), new XAttribute("lng", atm.lng)));

                xdoc.Save("ATM.xml");
                return 1;
            }
            catch { return 0; }
        }

        public static List<string> Bank_GetAllBank()
        {
            XDocument xdoc = XDocument.Load("ATM.xml");
            XElement root = xdoc.Root;
            var p = root.Elements().Where(c => c.Parent == root).Select(d => d.Attribute("name").Value.ToString()).ToList();
            p.Insert(0, "All");
            return p;
        }

        public static List<CPosATM> Bank_GetAllATM()
        {
            XDocument xdoc = XDocument.Load("ATM.xml");
            XElement root = xdoc.Root;

            return root.Elements("Bank").Descendants().Select(c => new CPosATM(c.Attribute("address").Value, c.Attribute("place").Value,
                c.Attribute("workinghour").Value, c.Attribute("lat").Value, c.Attribute("lng").Value, c.Attribute("image").Value, c.Parent.Attribute("name").Value)).ToList();
        }

        public static List<CPosATM> Bank_GetATMByBank(string BankName)
        {
            XDocument xdoc = XDocument.Load("ATM.xml");
            XElement root = xdoc.Root;
            return root.Elements("Bank").Where(c => c.Attribute("name").Value.ToLower() == BankName).Descendants()
                .Select(c => new CPosATM(c.Attribute("address").Value, c.Attribute("place").Value,
                c.Attribute("workinghour").Value, c.Attribute("lat").Value, c.Attribute("lng").Value, c.Attribute("image").Value, c.Parent.Attribute("name").Value)).ToList();
        }

        public static List<string> Hotel_GetAllDistrict()
        {
            XDocument xdoc = XDocument.Load("Hotel.xml");
            XElement root = xdoc.Root;
            var p = root.Elements().Where(c => c.Parent == root).Select(d => d.Attribute("name").Value).ToList();
            p.Insert(0, "All");
            return p;
        }

        public static List<CPosHotel> Hotel_GetAllHotel()
        {
            XDocument xdoc = XDocument.Load("Hotel.xml");
            XElement root = xdoc.Root;

            return root.Elements("District").Descendants().Select(c => new CPosHotel(c.Attribute("name").Value,c.Attribute("address").Value,
                c.Attribute("lat").Value,c.Attribute("lng").Value,c.Attribute("imageurl").Value,c.Attribute("detail").Value)).ToList();
        }

        public static List<CPosHotel> Hotel_GetHotelByDistrict(string DistrictName)
        {
            XDocument xdoc = XDocument.Load("Hotel.xml");
            XElement root = xdoc.Root;
            return root.Elements("District").Where(c => c.Attribute("name").Value == DistrictName).Descendants()
                .Select(c => new CPosHotel(c.Attribute("name").Value, c.Attribute("address").Value,
                c.Attribute("lat").Value, c.Attribute("lng").Value, c.Attribute("imageurl").Value, c.Attribute("detail").Value)).ToList();
        }

        public static List<CPosHotel> Hotel_GetHotelByStreet(string StreetName)
        {
            XDocument xdoc = XDocument.Load("Hotel.xml");
            XElement root = xdoc.Root;
            return root.Elements("District").Descendants().Where(c=>c.Attribute("address").Value.ToLower().Contains(StreetName))
                .Select(c => new CPosHotel(c.Attribute("name").Value, c.Attribute("address").Value,
                c.Attribute("lat").Value, c.Attribute("lng").Value, c.Attribute("imageurl").Value, c.Attribute("detail").Value)).ToList();
        }

        public static List<CPosHotel> Hotel_GetHotelByName(string HotelName)
        {
            XDocument xdoc = XDocument.Load("Hotel.xml");
            XElement root = xdoc.Root;
            return root.Elements("District").Descendants().Where(c => c.Attribute("name").Value.ToLower().Contains(HotelName))
                .Select(c => new CPosHotel(c.Attribute("name").Value, c.Attribute("address").Value,
                c.Attribute("lat").Value, c.Attribute("lng").Value, c.Attribute("imageurl").Value, c.Attribute("detail").Value)).ToList();
        }


    }
}
